package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.IcPostUserLike;
import com.ruoyi.system.domain.IcPosts;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.query.PostUserLikeQuery;
import com.ruoyi.system.domain.query.WeChatEventQuery;
import com.ruoyi.system.domain.query.WeChatLoginQuery;
import com.ruoyi.system.service.IIcPostsService;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.WeChatService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信相关api
 */
@RestController
@RequestMapping("/system/wechat")
public class WeChatController {

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private IIcPostsService postsService;


    @PostMapping("/login")
    public AjaxResult login(@RequestBody WeChatLoginQuery loginQuery) {
        AjaxResult result = weChatService.login(loginQuery);
        return result;
    }

    @GetMapping("/event/list/{iuId}")
    public AjaxResult getEventList(@PathVariable Long iuId) {
        AjaxResult result = weChatService.getEventListByIuId(iuId);
        return result;
    }

    @GetMapping("/event/detail/{eventId}")
    public AjaxResult getEventDetail(@PathVariable Long eventId) {
        AjaxResult result = weChatService.getEventDetailById(eventId);
        return result;
    }

    @PostMapping("/event/save")
    public AjaxResult saveEvent(@RequestBody WeChatEventQuery eventQuery) {
        AjaxResult result = weChatService.saveEventByQuery(eventQuery);
        return result;
    }

    @GetMapping("/notice/list")
    public AjaxResult getNoticeList() {
        List<SysNotice> sysNotices = noticeService.selectNoticeList(null);
        List<SysNotice> collect = sysNotices.stream()
                .filter(s -> StringUtils.equals(s.getStatus(), "0"))
                .sorted((o1, o2) -> {
                    if (ObjectUtils.isEmpty(o1.getCreateTime())) {
                        return 1;
                    }
                    if (ObjectUtils.isEmpty(o2.getCreateTime())) {
                        return -1;
                    }
                    return o2.getCreateTime().compareTo(o1.getCreateTime());
                })
                .collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

    @GetMapping("/notice/detail/{noticeId}")
    public AjaxResult getNoticeDetailById(@PathVariable Long noticeId) {
        SysNotice sysNotice = noticeService.selectNoticeById(noticeId);
        return AjaxResult.success(sysNotice);
    }

    @GetMapping("/posts/list")
    public AjaxResult getPostsList() {
        List<IcPosts> icPosts = postsService.selectIcPostsList(null);
        List<IcPosts> collect = icPosts.stream()
                .filter(p -> "0".equals(p.getStatus()))
                .collect(Collectors.toList());
        return AjaxResult.success(collect);
    }

    @PostMapping("/posts/add")
    public AjaxResult addPost(@RequestBody IcPosts posts) {
        AjaxResult result = weChatService.savePosts(posts);
        return result;
    }

    @GetMapping("/posts/detail/{postId}")
    public AjaxResult getPostsDetailById(@PathVariable Long postId) {
        AjaxResult result = weChatService.getPostsDetailById(postId);
        return result;
    }

    @GetMapping("/post/like")
    public AjaxResult getPostUserLike(PostUserLikeQuery likeQuery) {
        AjaxResult result = weChatService.getPostUserLike(likeQuery);
        return AjaxResult.success(result);
    }

    @PostMapping("/post/like")
    public AjaxResult addPostUserLike(@RequestBody PostUserLikeQuery likeQuery) {
        AjaxResult result = weChatService.addPostUserLike(likeQuery);
        return AjaxResult.success(result);
    }


}
